package com.lu.manage.modular.system.controller;

import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.common.node.ZTreeNode;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.model.Role;
import com.lu.manage.modular.system.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program LuBoot
 * @description: 角色控制器
 * @author: zhanglu
 * @create: 2019-11-05 20:13:00
 */
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    private static String PREFIX = "/modular/system/role";

    @Autowired
    private RoleService roleService;


/*******************************跳转页面相关*******************************/

    /**
     * 首页
     * @return
     */
    @GetMapping("")
    public Object index(){
        return PREFIX + "/role.html";
    }

    /**
     * 新增
     * @return
     */
    @GetMapping("/toAdd")
    public Object toAdd(){
        return PREFIX + "/role_add.html";
    }

    /**
     * 修改
     * @return
     */
    @GetMapping("/toUpdate")
    public Object toUpdate(String roleId, Model model){
        Role role = roleService.getById(roleId);
        if(ToolUtil.isNotEmpty(role)){
            Role pRole = roleService.getById(role.getPid());
            role.setPName(ToolUtil.isNotEmpty(pRole) ? pRole.getName() : SystemConstant.pid);
        }
        model.addAttribute("obj", role);
        return PREFIX + "/role_update.html";
    }

    /**
     * 跳转到权限分配
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @GetMapping(value = "/role_assign/{roleId}")
    public String roleAssign(@PathVariable("roleId") Long roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return PREFIX + "/role_assign.html";
    }


/*******************************操作按钮相关*******************************/

    /**
     * 新增菜单
     * @param role
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/role/add")
    public Object add(Role role) throws Exception{
        this.roleService.add(role);
        return new SuccessTip();
    }

    /**
     * 修改菜单
     * @param role
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/role/update")
    public Object update(Role role) throws Exception {

        //如果修改了编号，则该菜单的子菜单也要修改对应的父级(菜单层级修改情况)
        this.roleService.update(role);

        return new SuccessTip();
    }

    /**
     * 删除菜单
     * @param roleId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/role/delete")
    public Object delete(String roleId) throws Exception{
        this.roleService.delete(roleId);
        return new SuccessTip();
    }

    /**
     * 角色排序
     * @param nodes
     * @return
     * @throws Exception
     */
    @PostMapping("/sort")
    @ResponseBody
    @RequiresPermissions(value = "/system/role/sort")
    public Object sort(String nodes) throws Exception{
        this.roleService.sortAllRoles(nodes);
        return new SuccessTip();
    }


    /**
     * 配置权限
     * @param roleId
     * @param ids
     * @return
     */
    @PostMapping("/setAuthority")
    @ResponseBody
    @RequiresPermissions(value = "/system/role/setAuthority")
    public Object setAuthority(String roleId, String ids) throws Exception {
        this.roleService.setAuthority(roleId, ids);
        return new SuccessTip();
    }

/*******************************查询数据相关*******************************/

    /**
     * 角色列表分页
     * @param map 查询参数
     * @return
     */
    @GetMapping("/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> map){
        return roleService.listPage(map);
    }

    /**
     * 角色树
     * @return
     */
    @GetMapping("/roleTreeList")
    @ResponseBody
    public Object roleTreeList(){
        List<ZTreeNode> roleTreeList = (List<ZTreeNode>) roleService.roleTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    }

}
